package cn.jdy.plugins.wap;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jdy.framework.log.BaseLog;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

/**
 * @title 生成二维码
 * @author lutan
 */
@Controller
@RequestMapping("/createqcode")
public class CreateQcode {
	
	
	@Autowired
	protected HttpServletRequest request;
	/**
	 * 直播地址
	 * @param
	 * @return
	 */
	@RequestMapping("/directorUrl")
	@ResponseBody
	public Map<String, Object> directorUrl(Long liveId, String status) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		//获取域名
		//StringBuffer urls = request.getRequestURL();  
		//String str = urls.toString().split(":")[1];
		//String Url = str.substring(2, str.length());  
		String Url = request.getHeader("Host2") ;
		BaseLog.e(this.getClass(),"二维码生成域名"+Url );
		//String Url = urls.delete(urls.length() - request.getRequestURI().length(), urls.length()).toString();
		try {
			String url ="http://"+Url+"/w/live/index/"+liveId;
			jsonMap.put("url",url);
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "状态切换异常", e);
		}
		return jsonMap;
	}
	
	
	/**
	 * 生成二维码图片并直接以流的形式输出到页面
	 * 
	 * @param code_url
	 * @param response
	 */
	@RequestMapping("/qcode")
	@ResponseBody
	public void getQRCode(String url,HttpServletResponse response) {
		//String url ="http://127.0.0.1:8080/lives/liveIndex/2727";
		CreateQcode.encodeQrcode(url, response);
	}
	/**
	 * 生成二维码图片并直接以流的形式输出到页面
	 * 
	 * @param code_url
	 * @param response
	 */
	@RequestMapping("/qcodezhfl")
	public void qcodezhfl(String url , HttpServletResponse response) {
		//String url ="http://127.0.0.1:8080/lives/liveIndex/2727";
		String yel=url.replaceAll("]","&");
		CreateQcode.encodeQrcode(yel, response);
	}
	/**
	 * 生成二维码图片 不存储 直接以流的形式输出到页面
	 * 
	 * @param content
	 * @param response
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void encodeQrcode(String content, HttpServletResponse response) {
		if (content == null || "".equals(content))
			return;
		MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		Map hints = new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符集编码类型
		BitMatrix bitMatrix = null;
		try {
			bitMatrix = multiFormatWriter.encode(content,
					BarcodeFormat.QR_CODE, 300, 300, hints);
			BufferedImage image = toBufferedImage(bitMatrix);
			// 输出二维码图片流
			try {
				ImageIO.write(image, "png", response.getOutputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (WriterException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	/**
	 * 转换为图片
	 */
	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF;

	public static BufferedImage toBufferedImage(BitMatrix matrix) {
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
			}
		}
		return image;
	}
}
